package bomberman.entity;

import gameframework.base.DrawableImage;
import gameframework.base.Overlappable;

import java.awt.Canvas;
import java.awt.Point;
import java.awt.Graphics;
import java.util.Random;

import bomberman.BombermanGameLevel;


public class BreakableWall extends AbstractWall implements Overlappable{

	private static final int WALL_SPRITE_SIZE = 12;
	private static final int BONUS_PROBABILITY = 3;
	private static final int SPECIAL_BONUS_PROBABILITY = 10;
	private static final int VOMITO_BOMB_BONUS_PROBABILITY = 8;
	protected static DrawableImage image = null;
	private Canvas canvas;
	private int breakTimer = -1;
	private int spriteNumber = 0;
	
	public BreakableWall(Canvas defaultCanvas, Point pos) {
		super(defaultCanvas, pos);
		if (image == null) {
			image = new DrawableImage("images/BreakableWall.png", defaultCanvas);
		}
		canvas = defaultCanvas;
	}	

	public void draw(Graphics g) {
		g.drawImage(image.getImage(), (int) getPosition().getX(),
				(int) getPosition().getY(), (int) getPosition().getX()+ BombermanGameLevel.SPRITE_SIZE,
				(int) getPosition().getY() + BombermanGameLevel.SPRITE_SIZE, spriteNumber * WALL_SPRITE_SIZE,
				0, (spriteNumber + 1) * WALL_SPRITE_SIZE,
				WALL_SPRITE_SIZE, null);
	}

	@Override
	public void oneStepMoveHandler() {
		if(breakTimer-- == 0)
		{
			Random val = new Random();
			if (val.nextInt(BONUS_PROBABILITY) == 0)
			{
				if (val.nextInt(SPECIAL_BONUS_PROBABILITY)==0)
				{
					if (val.nextBoolean())
						BombermanGameLevel.medUniverse.getUniverse().addGameEntity(new BonusCatchBomb(canvas, getPos()));
					else
						BombermanGameLevel.medUniverse.getUniverse().addGameEntity(new BonusRandom(canvas, getPos()));
				}
				else
				{
					if (val.nextInt(VOMITO_BOMB_BONUS_PROBABILITY) == 0)
						BombermanGameLevel.medUniverse.getUniverse().addGameEntity(new BonusVomitoBomb(canvas, getPos()));
					else
					{
						if (val.nextBoolean())
							BombermanGameLevel.medUniverse.getUniverse().addGameEntity(new BonusIncreaseBombScope(canvas, getPos()));
						else
							BombermanGameLevel.medUniverse.getUniverse().addGameEntity(new BonusIncreaseNumberOfBombs(canvas, getPos()));
					}
				}
			}			
			BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(this);
		}
		else if(breakTimer >= 0)
			spriteNumber++;
	}
	
	public void breakWall(){
		breakTimer = 4;
	}
}
